<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>ZooKeeper分布式一致性深度解析</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        .content-card {
            background: rgba(255, 255, 255, 0.95);
            backdrop-filter: blur(10px);
            transition: all 0.3s ease;
        }
        .content-card:hover {
            transform: translateY(-2px);
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
        }
        .section-title {
            position: relative;
            padding-left: 20px;
        }
        .section-title::before {
            content: '';
            position: absolute;
            left: 0;
            top: 50%;
            transform: translateY(-50%);
            width: 4px;
            height: 24px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border-radius: 2px;
        }
        .highlight-box {
            background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
            color: white;
            padding: 20px;
            border-radius: 12px;
            margin: 20px 0;
        }
        .tech-badge {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            padding: 4px 12px;
            border-radius: 20px;
            font-size: 14px;
            display: inline-block;
            margin: 4px;
        }
        .mermaid {
            background: white;
            padding: 20px;
            border-radius: 12px;
            box-shadow: 0 4px 20px rgba(0, 0, 0, 0.05);
        }
        .drop-cap {
            float: left;
            font-size: 4em;
            line-height: 0.8;
            margin: 0.1em 0.1em 0 0;
            font-weight: 700;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            font-family: 'Noto Serif SC', serif;
        }
        .info-card {
            background: linear-gradient(135deg, #e0c3fc 0%, #8ec5fc 100%);
            padding: 24px;
            border-radius: 16px;
            margin: 20px 0;
            position: relative;
            overflow: hidden;
        }
        .info-card::before {
            content: '';
            position: absolute;
            top: -50%;
            right: -50%;
            width: 200%;
            height: 200%;
            background: radial-gradient(circle, rgba(255,255,255,0.2) 0%, transparent 70%);
            animation: shimmer 3s infinite;
        }
        @keyframes shimmer {
            0% { transform: rotate(0deg); }
            100% { transform: rotate(360deg); }
        }
        .timeline-item {
            position: relative;
            padding-left: 40px;
            margin-bottom: 30px;
        }
        .timeline-item::before {
            content: '';
            position: absolute;
            left: 10px;
            top: 8px;
            width: 12px;
            height: 12px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border-radius: 50%;
        }
        .timeline-item::after {
            content: '';
            position: absolute;
            left: 15px;
            top: 20px;
            width: 2px;
            height: calc(100% + 10px);
            background: #e0e0e0;
        }
        .timeline-item:last-child::after {
            display: none;
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <div class="hero-gradient text-white py-20">
        <div class="container mx-auto px-6">
            <div class="max-w-4xl mx-auto text-center">
                <h1 class="text-5xl md:text-6xl font-bold mb-6 leading-tight">
                    ZooKeeper如何实现<br>分布式一致性？
                </h1>
                <p class="text-xl md:text-2xl opacity-90 mb-8">
                    深入解析ZAB协议的核心机制与实现原理
                </p>
                <div class="flex flex-wrap justify-center gap-4">
                    <span class="tech-badge"><i class="fas fa-network-wired mr-2"></i>分布式系统</span>
                    <span class="tech-badge"><i class="fas fa-sync-alt mr-2"></i>一致性协议</span>
                    <span class="tech-badge"><i class="fas fa-server mr-2"></i>高可用架构</span>
                </div>
            </div>
        </div>
    </div>

    <!-- Main Content -->
    <div class="container mx-auto px-6 py-12">
        <!-- Introduction -->
        <div class="content-card rounded-2xl shadow-xl p-8 mb-8">
            <p class="text-lg leading-relaxed text-gray-700">
                <span class="drop-cap">Z</span>ooKeeper 作为一个高性能的分布式协调服务，提供了强一致性的数据存储和领导选举等功能，成为许多分布式应用程序的基础组件。那么，ZooKeeper 是如何实现分布式一致性的呢？答案在于其核心协议——ZAB（ZooKeeper Atomic Broadcast）协议。
            </p>
            <div class="info-card mt-6">
                <h3 class="text-xl font-bold mb-3 text-gray-800"><i class="fas fa-lightbulb mr-2"></i>核心洞察</h3>
                <p class="text-gray-700">ZAB协议是一种为ZooKeeper量身定制的分布式一致性协议，它专门解决了在不可靠的网络环境中，如何确保集群中数据的一致性和事务的全局有序性。</p>
            </div>
        </div>

        <!-- ZooKeeper Overview -->
        <div class="content-card rounded-2xl shadow-xl p-8 mb-8">
            <h2 class="section-title text-3xl font-bold mb-6 text-gray-800">ZooKeeper概述与分布式一致性需求</h2>
            
            <div class="grid md:grid-cols-2 gap-6 mb-8">
                <div class="bg-gradient-to-br from-purple-50 to-pink-50 p-6 rounded-xl">
                    <h3 class="text-xl font-semibold mb-4 text-gray-800"><i class="fas fa-cube mr-2 text-purple-600"></i>基本功能</h3>
                    <ul class="space-y-3">
                        <li class="flex items-start">
                            <i class="fas fa-check-circle text-purple-600 mt-1 mr-3"></i>
                            <div>
                                <strong>节点创建和删除</strong>：创建和删除 znode
                            </div>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-check-circle text-purple-600 mt-1 mr-3"></i>
                            <div>
                                <strong>节点数据读写</strong>：读取或更新 znode 的数据
                            </div>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-check-circle text-purple-600 mt-1 mr-3"></i>
                            <div>
                                <strong>监听机制</strong>：客户端可以对 znode 设置监听器
                            </div>
                        </li>
                    </ul>
                </div>
                
                <div class="bg-gradient-to-br from-blue-50 to-indigo-50 p-6 rounded-xl">
                    <h3 class="text-xl font-semibold mb-4 text-gray-800"><i class="fas fa-shield-alt mr-2 text-blue-600"></i>一致性挑战</h3>
                    <ul class="space-y-3">
                        <li class="flex items-start">
                            <i class="fas fa-exclamation-triangle text-blue-600 mt-1 mr-3"></i>
                            <div>
                                <strong>配置管理</strong>：多节点配置同步
                            </div>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-exclamation-triangle text-blue-600 mt-1 mr-3"></i>
                            <div>
                                <strong>领导选举</strong>：快速一致的选举机制
                            </div>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-exclamation-triangle text-blue-600 mt-1 mr-3"></i>
                            <div>
                                <strong>分布式锁</strong>：资源访问的互斥控制
                            </div>
                        </li>
                    </ul>
                </div>
            </div>

            <!-- Architecture Diagram -->
            <div class="mermaid">
                graph TB
                    subgraph "ZooKeeper集群"
                        L[Leader节点]
                        F1[Follower节点1]
                        F2[Follower节点2]
                        F3[Follower节点3]
                    end
                    
                    subgraph "客户端"
                        C1[客户端1]
                        C2[客户端2]
                        C3[客户端3]
                    end
                    
                    C1 -->|写请求| L
                    C2 -->|读请求| F1
                    C3 -->|读请求| F2
                    
                    L -->|事务广播| F1
                    L -->|事务广播| F2
                    L -->|事务广播| F3
                    
                    style L fill:#667eea,stroke:#fff,stroke-width:3px,color:#fff
                    style F1 fill:#8ec5fc,stroke:#fff,stroke-width:2px,color:#fff
                    style F2 fill:#8ec5fc,stroke:#fff,stroke-width:2px,color:#fff
                    style F3 fill:#8ec5fc,stroke:#fff,stroke-width:2px,color:#fff
            </div>
        </div>

        <!-- ZAB Protocol Concept -->
        <div class="content-card rounded-2xl shadow-xl p-8 mb-8">
            <h2 class="section-title text-3xl font-bold mb-6 text-gray-800">ZAB协议的概念</h2>
            
            <div class="highlight-box">
                <h3 class="text-2xl font-bold mb-3"><i class="fas fa-broadcast-tower mr-2"></i>ZAB协议核心</h3>
                <p class="text-lg">ZAB（ZooKeeper Atomic Broadcast）协议是ZooKeeper的核心协议，专门设计用于解决分布式系统中数据一致性的问题。它兼顾了分布式系统的两个关键属性：<strong>高可用性</strong>和<strong>强一致性</strong>。</p>
            </div>

            <div class="grid md:grid-cols-2 gap-6 mt-8">
                <div class="bg-gray-50 p-6 rounded-xl">
                    <h4 class="text-lg font-semibold mb-4"><i class="fas fa-crown mr-2 text-yellow-600"></i>领导选举模式</h4>
                    <p class="text-gray-700">当集群启动或Leader失效时，通过选举产生唯一的Leader节点，负责接收和处理所有写请求。</p>
                </div>
                <div class="bg-gray-50 p-6 rounded-xl">
                    <h4 class="text-lg font-semibold mb-4"><i class="fas fa-satellite-dish mr-2 text-green-600"></i>原子广播模式</h4>
                    <p class="text-gray-700">Leader将事务请求以一致的顺序广播给所有Follower节点，确保数据一致性。</p>
                </div>
            </div>

            <!-- ZAB Goals -->
            <div class="mt-8">
                <h3 class="text-2xl font-semibold mb-4">ZAB协议的目标</h3>
                <div class="space-y-4">
                    <div class="timeline-item">
                        <h4 class="font-semibold text-lg mb-2">数据一致性</h4>
                        <p class="text-gray-700">确保所有数据更新在集群的所有节点上以相同的顺序进行应用，实现强一致性。</p>
                    </div>
                    <div class="timeline-item">
                        <h4 class="font-semibold text-lg mb-2">容错性</h4>
                        <p class="text-gray-700">在部分节点失效或网络分区时，继续提供服务，提高系统的可用性。</p>
                    </div>
                    <div class="timeline-item">
                        <h4 class="font-semibold text-lg mb-2">快速恢复</h4>